﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data;
using System.Windows.Forms; 

namespace SmartMD.DataAccess.Oracle
{
    public partial class ConnectDatabase : IConnection
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataString">数据库连接字符串</param>
        public ConnectDatabase()
        {

        }
        private static ConnectDatabase inst = null;
        public static ConnectDatabase getInstance()
        {
            if (inst == null)
            {
                inst = new ConnectDatabase();
            }
            return inst;
        }
        public void setConnetion(string DataString)
        {
            if (DataString == null)
                return;
            oledbCon = new OleDbConnection(DataString);
            oledbCon.Open();
        }
        public void closeConnetion()
        {
            if (oledbCon != null)
            {
                oledbCon.Close();
                oledbCon = null;
            }
        }
        private OleDbConnection oledbCon = null;
    
        public DataTable Select(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                if (oledbCon == null)
                    return null;
                //打开数据库连接
                XMLHelper xmlOper = new XMLHelper();
                String dataAccess = null;
                if (oledbCon.State == ConnectionState.Closed)
                {
                    String dataProvider = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='dataProvider']", "value");
                    if (dataProvider == "Oracle")
                    {
                        dataAccess = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='DataAddress']", "value");
                        ConnectDatabase.getInstance().setConnetion(dataAccess);
                    }
                }
                OleDbDataAdapter oledbApt = new OleDbDataAdapter(sql, oledbCon);
                oledbApt.Fill(dt); 
                return dt;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return dt;
            }
        }

        public Boolean Update(string sql)
        {
            try
            {
                if (oledbCon == null)
                    return false;
                OleDbCommand oledbCom = new OleDbCommand(sql, oledbCon);
                int d = oledbCom.ExecuteNonQuery();
          
                if (d < 1)
                    return false;
                return true;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public Boolean Insert(string sql)
        {
            try
            {
                if (oledbCon == null)
                    return false;


                //打开数据库连接
                XMLHelper xmlOper = new XMLHelper();
                String dataAccess = null;
                if (oledbCon.State == ConnectionState.Closed)
                {
                    String dataProvider = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='dataProvider']", "value");
                    if (dataProvider == "Oracle")
                    {
                        dataAccess = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='DataAddress']", "value");
                        ConnectDatabase.getInstance().setConnetion(dataAccess);
                    }
                }
                OleDbCommand oledbCom = new OleDbCommand(sql, oledbCon);
                int d = oledbCom.ExecuteNonQuery();
                oledbCon.Dispose();
                oledbCon.Close();

                if (d < 1)
                    return false;
                return true;
            }
            catch(Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public Boolean Delete(string sql)
        {

            XMLHelper xmlOper = new XMLHelper();
            String dataAccess = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='DataAddress']", "value");
            if (dataAccess == null)
                return false;
            OleDbConnection conn = new OleDbConnection(dataAccess);
            conn.Open();
            try
            {
                if (conn == null)
                    return false;
                OleDbCommand oledbCom = new OleDbCommand(sql, conn);
                int d = oledbCom.ExecuteNonQuery();
                if (d < 1)
                    return false;
                return true;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            finally {
                conn.Close();
            }
        }
    }
      
}
